package org.eclipse.epsilon.emc.jdt2;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import org.eclipse.epsilon.eol.execute.operations.AbstractOperation;
import org.eclipse.epsilon.eol.execute.operations.declarative.IAbstractOperationContributor;
import org.eclipse.jdt.core.IJavaProject;

public class QuickList extends ArrayList implements IAbstractOperationContributor {

	private String type;
	private ASTReflection astModel;
	private List<IJavaProject> javaProjects;
	private boolean isPopulated = false;

	
	private static final long serialVersionUID = 9114555249146769973L;

	public QuickList(String type, ASTReflection astModel,
			List<IJavaProject> javaProjects) {
		super();
		this.type = type;
		this.astModel = astModel;
		this.javaProjects = javaProjects;
	}

	public QuickList(String type, ASTReflection astModel,
			List<IJavaProject> javaProjects, boolean isPopulated) {
		super();
		this.type = type;
		this.astModel = astModel;
		this.javaProjects = javaProjects;
		this.isPopulated = isPopulated;
	}

	@Override
	public AbstractOperation getAbstractOperation(String name) {
		// TODO Auto-generated method stub
		System.out.println("#########" + name);
		if (name.equals("select")) {
			return new QuickSelectOperationBase(type, astModel, javaProjects);
		}
		if (name.equals("selectOne")) {
			return new QuickSelectOneOperation(type, astModel, javaProjects);
		}
		return null;
	}

	@Override
	public Iterator iterator() {
		// TODO Auto-generated method stub
		System.out.println("ITERATOR");
		if (isPopulated) {
			System.out.println("!!!!UNMark");
			return super.iterator();
		} else {
			addAll(SearchUtil.search(type, astModel, javaProjects, "*", true));
			isPopulated = true;
			System.out.println("!!!mark");
			return super.iterator();
		}
		// return super.iterator();
	}

	@Override
	public int size() {
		// TODO Auto-generated method stub
		System.out.println("size");
		if (isPopulated) {
			System.out.println("size: is populated");
			return super.size();
		} else {
			System.out.println("size: not populated");
			if(type.equals(JdtModel.TYPE_DEC)){
				
			}
			addAll(SearchUtil.search(type, astModel, javaProjects, "_size_", true));
			isPopulated = true;
			return super.size();
		}
	}

}
